package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.EmployeeDao;
import cn.gson.crm.model.dao.UserDao;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserDao userDao;
    @Autowired
    EmployeeDao employeeDao;
    //查询所有员工
    public Map<String,Object> selectuser(Integer page, Integer size){
        List<User> userList=userMapper.selectuser();
        //查询所有部门
        List<Dept> deptList=userMapper.selectdept();
        Map<String,Object> map = new HashMap<>();
        map.put("rows",userList);
        map.put("deptList",deptList);
        return map;
    };
    //根据部门查询职位
    public List<Role> selectrow(Integer did){
        return userMapper.selectrow(did);
    };
    //修改员工为离职状态
    public void updateyuan(Integer yid){
      userMapper.updateyuan(yid);
    };
    //根据员工id查询员工详情
    public List<User>  selectuserxiang(Integer yid){
      return   userMapper.selectuserxiang(yid);
    };
    //条件查询员工
    public Map<String,Object> selectwhere(String where,Integer page, Integer size){
        List<User> userList=userMapper.selectwhere(where);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",userList);
        return map;
    };
    public Map<String,Object> selectDate( String statr,  String endtime,Integer page, Integer size){

        List<User> userList=userMapper.selectDate(statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",userList);
        return map;
    };
    //新增员工
    public void saveuser(User user){
        List<Role> roleList = user.getRoleList();
        User save = userDao.save(user);
        User user1 = userDao.findById(save.getyId()).get();
        user1.setRoleList(roleList);
        Employee employee=new Employee();

        employee.setYname(user.getyName());
        employee.setUser(user1);
        employeeDao.save(employee);
    }
    //查询员工根据姓名
    public List<User> selectyname(String yname){
        return userMapper.selectyname(yname);
    };
    //登录查看是否存在
    public List<User> selectfoucun(String yname,String ypass){
        return userMapper.selectfoucun(yname,ypass);
    };
    public List<EmployeerVo> selectFonks(Integer uid){
        List<EmployeerVo> employeer = userMapper.firstRouter();
        List<EmployeerVo> sorts = userMapper.selectFonks(uid);
        for(EmployeerVo s:sorts){
            String[] split = s.getComponent().split("/");
            s.setName(split[split.length-1]);
        }
        for(EmployeerVo v:employeer){
            String[] split=v.getComponent().split("/");
            v.setName(split[split.length-1]);
            v.setChildren(sorts);
        }
        return employeer;
    };
    public List<Sort> faFun(Integer uid){
        List<Sort> sorts=userMapper.selecctluyou(uid);
        return sorts;
    }
    //按钮查询
    public List<String> selectanniu(Integer uid){
        List<String> sorts = userMapper.selectanniu(uid);

        return sorts;
    };

}
